//
//  DebugSandBoxFileDetailViewController.m
//  YK_DebugManager
//
//  Created by edward on 2021/6/3.
//

#import "DebugSandBoxFileDetailViewController.h"
#import "DebugManagerUtil.h"
#import <QuickLook/QuickLook.h>

@interface DebugSandBoxFileDetailViewController ()<UITextViewDelegate,QLPreviewControllerDelegate,QLPreviewControllerDataSource>
///
@property(nonatomic,copy) NSString *filePath;
/***/
@property (nonatomic, strong) UITextView *textView;
@end

@implementation DebugSandBoxFileDetailViewController

- (instancetype)initWithFilePath:(NSString *)filePath
{
    self = [super init];
    if (self) {
        self.filePath = filePath;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //    DoraemonSanboxDetailViewController
    self.title = @"文件预览";
    
    if (self.filePath.length > 0) {
        NSString *path = self.filePath;
        if ([path hasSuffix:@".strings"] || [path hasSuffix:@".plist"]) {
            // 文本文件
            [self.view addSubview:self.textView];
            self.textView.text = [[NSDictionary dictionaryWithContentsOfFile:path] description];
            
        } else if ([path hasSuffix:@".DB"] || [path hasSuffix:@".db"] || [path hasSuffix:@".sqlite"] || [path hasSuffix:@".SQLITE"] || [self isSQLiteFile:self.filePath]) {
            // 数据库文件
//            [self browseDBTable];
            NSLog(@"🔥暂不支持该类型");
        } else if ([[path lowercaseString] hasSuffix:@".webp"]) {
            // webp文件
//            DoraemonWebpHandleBlock block = [DoraemonManager shareInstance].webpHandleBlock;
//            if (block) {
//                UIImage *img = [DoraemonManager shareInstance].webpHandleBlock(path);
//                [self setOriginalImage:img];
//            } else {
//                [self setContent:@"webp need implement webpHandleBlock in DoraemonManager"];
//            }
            NSLog(@"🔥暂不支持该类型");
        } else {
            // 其他文件 尝试使用 QLPreviewController 进行打开
            QLPreviewController *previewController = [[QLPreviewController alloc] init];
            previewController.delegate = self;
            previewController.dataSource = self;
            [self presentViewController:previewController animated:YES completion:^{
                
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
    } else {
//        [DoraemonToastUtil showToast:DoraemonLocalizedString(@"文件不存在") inView:self.view];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return NO;
}

- (UITextView *)textView
{
    if (!_textView) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, debug_screenW - 20, debug_screenH - debug_top_Height - debug_bottom_safe)];
        _textView.delegate = self;
        
    }
    return _textView;
}

#pragma mark - QLPreviewControllerDataSource, QLPreviewControllerDelegate
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
    return 1;
}
- (id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
    return [NSURL fileURLWithPath:self.filePath];
}
- (void)previewControllerDidDismiss:(QLPreviewController *)controller {
//    [self leftNavBackClick:nil];
}

- (BOOL)isSQLiteFile:(NSString *)file {
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:file];
    if (!fileHandle) {
        return NO;
    }
    NSData *data = [fileHandle readDataOfLength:16];
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    [fileHandle closeFile];
    if ([str isEqual:@"SQLite format 3\0"]) {
        return YES;
    } else {
        return NO;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
